package com.zaofan.mobile.controller;

import java.io.IOException;
import java.util.Collections;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.common.collect.Lists;
import com.zaofan.bean.OpenArea;
import com.zaofan.bean.OpenCity;
import com.zaofan.cache.CacheManage;
import com.zaofan.service.OpenCityService;
import com.zaofan.utils.JsonUtils;

@WebServlet("/mobile/area")
public class AreaController extends BaseServlet {

	private static final long serialVersionUID = 5884947841079014459L;
	private OpenCityService cityService = new OpenCityService();

	/**
	 * 城市 开通区域 列表
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	protected void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			String cityName = request.getParameter("cityName");
			if(cityName == null){
				JsonUtils.writeListFailJson("缺少城市信息", response);
				return;
			}
			OpenCity city = cityService.findByCityName(cityName);
			if(city == null){
				JsonUtils.writeListFailJson("对不起,暂未开通该城市,请选择其他城市试试!!", response);
				return;
			}
			if (CacheManage.openAreas.containsKey(city.getId())) {
				JsonUtils.writeSuccessJson("查询成功", CacheManage.openAreas.get(city.getId()), response);
			} else {
				JsonUtils.writeSuccessJson("查询成功", Collections.EMPTY_LIST, response);
			}
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeListFailJson("查询失败", response);
		}
		JsonUtils.writeListFailJson("查询失败!!" + ",未知错误", response);
	}
	
}
